package com.slofzx.nuoda.evaluation.project.service.impl;

import com.slofzx.nuoda.common.util.IdGen;
import com.slofzx.nuoda.evaluation.project.entity.ProjectReportDetailYear;
import com.slofzx.nuoda.evaluation.project.dto.ProjectReportDetailYearDTO;
import com.slofzx.nuoda.evaluation.project.util.CglibUtils;
import com.slofzx.nuoda.evaluation.project.vo.ProjectReportDetailYearVO;
import com.slofzx.nuoda.evaluation.project.dao.ProjectReportDetailYearDAO;
import com.slofzx.nuoda.evaluation.project.service.ProjectReportDetailYearService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.slofzx.nuoda.common.enums.CommonResultEnum;
import com.slofzx.nuoda.common.enums.ServiceResult;
import com.slofzx.nuoda.common.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 评价结果年份数据服务实现类
 *
 * @author nuoda
 * @since 2023-12-27 17:40:27
 */
@Service("projectReportDetailYearService")
public class ProjectReportDetailYearServiceImpl extends BaseServiceImpl implements ProjectReportDetailYearService {
    @Autowired
    private ProjectReportDetailYearDAO projectReportDetailYearDAO;

    /**
     * 将数据表数据转换为返回数据类型
     *
     * @param entity 数据表对应类
     * @return 实例对象
     */
    protected ProjectReportDetailYearVO parseEntity2VO(ProjectReportDetailYear entity) {
        ProjectReportDetailYearVO vo = new ProjectReportDetailYearVO();
//        vo.setId(entity.getId());
//        vo.setProjectId(entity.getProjectId());
//        vo.setYear(entity.getYear());
//        vo.setNetProfit(entity.getNetProfit());
//        vo.setTotalNetProfit(entity.getTotalNetProfit());
//        vo.setCashFlowAfterTax(entity.getCashFlowAfterTax());
//        vo.setTotalOperateCost(entity.getTotalOperateCost());
//        vo.setComputeVersionId(entity.getComputeVersionId());
//        vo.setOutPutYear(entity.getOutPutYear());
        CglibUtils.copy(entity,vo);
        return vo;
    }

    /**
     * 将数据传递类转换为数据表数据类
     *
     * @param dto 数据传递类实例
     * @return 实例对象
     */
    protected ProjectReportDetailYear parseDTO2Entity(ProjectReportDetailYearDTO dto) {
        ProjectReportDetailYear entity = new ProjectReportDetailYear();
//        entity.setId(dto.getId());
//        entity.setProjectId(dto.getProjectId());
//        entity.setComputeVersionId(dto.getComputeVersionId());
//        entity.setYear(dto.getYear());
//        entity.setNetProfit(dto.getNetProfit());
//        entity.setTotalNetProfit(dto.getTotalNetProfit());
//        entity.setCashFlowAfterTax(dto.getCashFlowAfterTax());
//        entity.setTotalOperateCost(dto.getTotalOperateCost());
//        entity.setOutPutYear(dto.getOutPutYear());
        CglibUtils.copy(dto,entity);
        return entity;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ProjectReportDetailYearVO queryById(String id) {
        ProjectReportDetailYear entity = this.projectReportDetailYearDAO.selectById(id);
        if (entity == null) {
            return null;
        }
        return parseEntity2VO(entity);
    }

    /**
     * 查询多条数据
     *
     * @param dto 数据传递类实例对象
     * @return 对象列表
     */
    @Override
    public List<ProjectReportDetailYearVO> queryList(ProjectReportDetailYearDTO dto) {
        //mybatis-plus 写法示例
        //IPage<ProjectReportDetailYear> pageResult = this.evaModelDAO.selectPage(new Page<>(dto.getPageNo(),dto.getPageSize()),null);
        //List<ProjectReportDetailYear> dataList = pageResult.getRecords();
        //dto.setCount((int) pageResult.getTotal());
        List<ProjectReportDetailYear> dataList = this.projectReportDetailYearDAO.queryList(dto);
        if (null == dataList || dataList.isEmpty()) {
            return Collections.emptyList();
        }
        List<ProjectReportDetailYearVO> resultList = new ArrayList(dataList.size());
        for (ProjectReportDetailYear item : dataList) {
            ProjectReportDetailYearVO vo = parseEntity2VO(item);
            resultList.add(vo);
        }
        // TODO 添加其他关联查询数据
        return resultList;
    }

    /**
     * 新增数据
     *
     * @param dto 实例对象
     * @return 操作结果
     */
    @Override
    public ServiceResult create(ProjectReportDetailYearDTO dto) {
        // TODO 添加校验逻辑
        ProjectReportDetailYear entity = parseDTO2Entity(dto);
        preInsert(entity);
        this.projectReportDetailYearDAO.insert(entity);
        return CommonResultEnum.SUCCESS;
    }

    @Override
    public ServiceResult batchSave(Collection<ProjectReportDetailYearDTO> dtos,String userId) {
        if (CollectionUtils.isEmpty(dtos)) {
            return CommonResultEnum.SUCCESS;
        }
        final List<ProjectReportDetailYear> collect = dtos.stream()
                .filter(Objects::nonNull)
                .map(m->{
                    final ProjectReportDetailYear projectReportDetailYear = this.parseDTO2Entity(m);
                    projectReportDetailYear.setCreateUser(userId);
                    projectReportDetailYear.setCreateTime(new Date());
                    projectReportDetailYear.setId(IdGen.uuid());
                    return projectReportDetailYear;
                })
                .collect(Collectors.toList());
        saveBatch(collect,projectReportDetailYearDAO,ProjectReportDetailYear.class);
        return CommonResultEnum.SUCCESS;
    }

    /**
     * 修改数据
     *
     * @param dto 实例对象
     * @return 操作结果
     */
    @Override
    public ServiceResult update(ProjectReportDetailYearDTO dto) {
        String id = dto.getId();
        if (StringUtils.isEmpty(id)) {
            return CommonResultEnum.NOT_EXIST;
        }
        ProjectReportDetailYear entity = this.projectReportDetailYearDAO.selectById(id);
        if (entity == null) {
            return CommonResultEnum.NOT_EXIST;
        }
        // TODO 添加其他校验逻辑，如数据权限

//        entity.setProjectId(dto.getProjectId());
//        entity.setYear(dto.getYear());
//        entity.setNetProfit(dto.getNetProfit());
//        entity.setTotalNetProfit(dto.getTotalNetProfit());
//        entity.setCashFlowAfterTax(dto.getCashFlowAfterTax());
//        entity.setTotalOperateCost(dto.getTotalOperateCost());
        CglibUtils.copy(dto,entity);
        this.projectReportDetailYearDAO.updateById(entity);
        return CommonResultEnum.SUCCESS;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 操作结果
     */
    @Override
    public ServiceResult deleteById(String id) {
        // TODO 添加校验逻辑，如数据权限
        this.projectReportDetailYearDAO.deleteById(id);
        return CommonResultEnum.SUCCESS;
    }
}
